package HelperFinancial.User;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import HelperFinancial.Class.MesesDoAno;
import HelperFinancial.DataBase.ConfigurationAdapter;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class DateUpdateForGraphicsActivity extends Activity {
	private DateUpdateForGraphicsActivity frame = this;
	private EditText dateInit;
	private Spinner spinnerMonth;
	private Spinner spinnerYear;
	private String mes;
	private String ano;
	int mYear;
	private int mMonth;
	private int mDay;

	static final int DATE_INIT_ID = 0;
	static final int DATE_END_ID = 1;

	/** Called when the activity is first create */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update_date_for_graphic);

//		dateEnd = (EditText) findViewById(R.id.camp_date_end_update);
		//dateInit = (EditText) findViewById(R.id.date_filter);
		Button save = (Button) findViewById(R.id.save_date_update_graphic);
		save.setOnClickListener(onSave);
		
		spinnerMonth = (Spinner) findViewById(R.id.date_update_spinner_graphic);
        
        ArrayAdapter<String> adapterMonth = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listaDeMeses());  
        adapterMonth.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerMonth.setAdapter(adapterMonth);
        spinnerMonth.setSelection(new Date().getMonth());
        spinnerMonth.setOnItemSelectedListener(new OnItemSelectedListener() {  
        	 @Override  
        	 public void onItemSelected(AdapterView parent, View arg1,  
        	   int arg2, long arg3) {
        		 for (int i = 0; i < MesesDoAno.values().length; i++) {
					if (MesesDoAno.values()[i].getName().equalsIgnoreCase(parent.getSelectedItem().toString())) {
						mes = MesesDoAno.values()[i].getValue();
					}
				}
        	 }  
        	 @Override  
        	 public void onNothingSelected(AdapterView parent) {
        		 GregorianCalendar today = new GregorianCalendar();
        		 mes = today.get(Calendar.MONTH) + "";
        	 }  
        });
        
		spinnerYear = (Spinner) findViewById(R.id.year_update_spinner_graphic);
        
        ArrayAdapter<String> adapterYear = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listaDeAnos());  
        adapterYear.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerYear.setAdapter(adapterYear);
        spinnerYear.setSelection(new GregorianCalendar().get(Calendar.YEAR) - 2005);
        spinnerYear.setOnItemSelectedListener(new OnItemSelectedListener() {  
        	 @Override  
        	 public void onItemSelected(AdapterView parent, View arg1,  
        	   int arg2, long arg3) {  
	        	 ano = (String)  parent.getSelectedItem();
        	 }  
        	 @Override  
        	 public void onNothingSelected(AdapterView parent) {
        		 GregorianCalendar today = new GregorianCalendar();
        		 ano = today.get(Calendar.YEAR) + "";
        	 }  
        });


		// get the current date
		final Calendar calendar = Calendar.getInstance();
		mYear = calendar.get(Calendar.YEAR);
		mMonth = calendar.get(Calendar.MONTH);
		mDay = calendar.get(Calendar.DAY_OF_MONTH);
 
		
	}

	private String[] listaDeAnos() {
		String[] lista = new String[46];
		for (int i = 2005; i <= 2050; i++) {
			lista[i-2005] = i+"";
		}
		return lista;
	}

	/**
	 * variavel privada que define o evento salvar.
	 */
	private View.OnClickListener onSave = new View.OnClickListener() {
		public void onClick(View v) {
			ConfigurationAdapter config = ConfigurationAdapter
					.getInstance(frame);
			String id = config.getId();
			config.updateDateInit(mes+"/"+ano, id);
			config.updateCategory(config.getCategory(), id);
			finish();
//			config.updateDateEnd(dateEnd.getText().toString().trim(), id);
			startActivity(new Intent(frame, GraphicActivity.class));
		}
	};

	// updates the date in the TextView
	private void updateDisplay(int flag) {
		String day = "";
		String month = "";
		if (mDay < 9) {
			day = "0" + mDay;
		} else {
			day += mDay;
		}
		if (mMonth < 9) {
			month = "0" + (mMonth + 1);
		} else {
			month += (mMonth + 1);
		}

		if (flag == DATE_INIT_ID) {
			dateInit.setText(new StringBuilder().append(day).append("/")
			// Month is 0 based so add 1
					.append(month).append("/").append(mYear).append(" "));
		} 
		
//		else if (flag == DATE_END_ID) {
//			dateEnd.setText(new StringBuilder().append(day).append("/")
//			// Month is 0 based so add 1
//					.append(month).append("/").append(mYear).append(" "));
//		}

	}

	// the callback received when the user "sets" the date in the dialog
	private DatePickerDialog.OnDateSetListener paymentSetListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateDisplay(DATE_INIT_ID);
		}
	};

	private DatePickerDialog.OnDateSetListener realizationSetListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateDisplay(DATE_END_ID);
		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_INIT_ID:
			return new DatePickerDialog(this, paymentSetListener, mYear,
					mMonth, mDay);
		case DATE_END_ID:
			return new DatePickerDialog(this, realizationSetListener, mYear,
					mMonth, mDay);
		}
		return null;
	}
	
	public String[] listaDeMeses(){
		String[] lista = new String[12];
		for (int i = 0; i < MesesDoAno.values().length; i++) {
			lista[i] = MesesDoAno.values()[i].getName();
		}
		return lista;
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
}